package com.frejoys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.dto.common.login.LoginListDto;
import com.frejoys.dao.entity.Login;
import com.frejoys.dao.mapper.LoginMapper;
import com.frejoys.service.LoginService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements LoginService {


    @Override
    public Login getInfo(Integer id) {
        LambdaQueryWrapper<Login> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Login::getId, id);
        return getOnly(wrapper);
    }


    @Override
    public List<Login> listInfo(LoginListDto dto) {
        Page page = new Page<>(dto.getPage(), dto.getSize());

        page.setSearchCount(false);
        LambdaQueryWrapper<Login> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getType() != null, Login::getType, dto.getType());
        Page pageData = baseMapper.selectPage(page, queryWrapper);
        return pageData.getRecords();
    }

    @Override
    public String getUserOpenid(Long userId, Integer type, Integer site) {
        LambdaQueryWrapper<Login> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Login::getOpenid).eq(Login::getUserId, userId).eq(Login::getType, type).eq(Login::getSite, site);
        Login e = getOnly(wrapper);
        if (e != null) {
            return e.getOpenid();
        }
        return null;
    }

}
